package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.time_pick.DateAndTimePickerDialog;
import com.wwwarehouse.common.tools.DateUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.customview.AutoClickButton;
import com.wwwarehouse.contract.event.EffectTimeEvent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * 生效时间Fragment
 * Created by ShineF on 2017/10/17 0017.
 */

public class EffectTimeFragment extends TheParentFragment implements View.OnClickListener {

    private TextView mEndEffect;
    private TextView mStartTime;
    private TextView mTimingEffect;
    private TextView mEndTime;
    private LinearLayout mLLStartTime;
    private LinearLayout mLLEndTime;
    private String startTime = null;
    private String endTime = null;
    private String validType = null;
    private AutoClickButton mConfirm;
    private String pbUkid;
    private String buId;
    private String buName;


    @Override
    protected int getContentId() {
        return R.layout.fragment_release_effect_time;
    }

    @Override
    protected void init(View view) {
        mEndEffect = (TextView) view.findViewById(R.id.tv_publish_end_effect);
        mTimingEffect = (TextView) view.findViewById(R.id.timing_effect);
        mStartTime = (TextView) view.findViewById(R.id.tv_strat_time);
        mEndTime = (TextView) view.findViewById(R.id.tv_end_time);
        mLLStartTime = (LinearLayout) view.findViewById(R.id.effect_start_time);
        mLLEndTime = (LinearLayout) view.findViewById(R.id.effect_end_time);
        mConfirm = (AutoClickButton) view.findViewById(R.id.confirm_btn);
        Bundle bundle = getArguments();
        if (bundle != null) {
            pbUkid = bundle.getString("pbUkid");
            buId = bundle.getString("buId");
            buName = bundle.getString("buName");
            validType = bundle.getString("validType");
            startTime = bundle.getString("start");
            endTime = bundle.getString("end");

        }
        mEndEffect.setSelected(false);
        mTimingEffect.setSelected(false);
        mEndEffect.setOnClickListener(this);
        mTimingEffect.setOnClickListener(this);
        mStartTime.setOnClickListener(this);
        mEndTime.setOnClickListener(this);
        mConfirm.setOnClickListener(this);
        if (validType != null) {
            if ("1".equals(validType)) {
                mTimingEffect.setSelected(false);
                mEndEffect.setSelected(true);
                mLLEndTime.setVisibility(View.VISIBLE);
                mLLStartTime.setVisibility(View.GONE);
                mEndTime.setText(endTime);
                mConfirm.setEnabled(true);
            } else if ("2".equals(validType)) {
                mTimingEffect.setSelected(true);
                mEndEffect.setSelected(false);
                mLLEndTime.setVisibility(View.VISIBLE);
                mLLStartTime.setVisibility(View.VISIBLE);
                mEndTime.setText(endTime);
                mStartTime.setText(startTime);
                mConfirm.setEnabled(true);

            }
        } else {
            defaultState();
        }

    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof EffectTimeFragment) {
            mActivity.setTitle(getString(R.string.set_effect_time));
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (TextUtils.equals("0", commonClass.getCode())) {
            String pbukid = commonClass.getData().toString();
            popFragment();
            //发送EventBus到拼卡页面
            // 生效类型【1、发布后生效 2、定时生效】
            if (TextUtils.isEmpty(pbukid)) {
                return;
            }
            if ("1".equals(validType)) {
                EffectTimeEvent effectTimeEvent = new EffectTimeEvent(pbukid, validType, "", mEndTime.getText().toString().trim());
                EventBus.getDefault().post(effectTimeEvent);
            } else if ("2".equals(validType)) {
                EffectTimeEvent effectTimeEvent = new EffectTimeEvent(pbukid, validType, mStartTime.getText().toString().trim(), mEndTime.getText().toString().trim());
                EventBus.getDefault().post(effectTimeEvent);
            }

        } else {
            toast(commonClass.getMsg());
        }

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_publish_end_effect) {
            defaultState();

        } else if (i == R.id.timing_effect) {
            mTimingEffect.setSelected(true);
            mEndEffect.setSelected(false);
            mLLEndTime.setVisibility(View.VISIBLE);
            mLLStartTime.setVisibility(View.VISIBLE);
            mEndTime.setText(getString(R.string.select_time));
            mStartTime.setText(getString(R.string.select_time));
            mConfirm.setEnabled(false);
            startTime = null;
            endTime = null;
            validType = "2";

        } else if (i == R.id.tv_strat_time) {
            new DateAndTimePickerDialog.Builder(getActivity())
                    .setData(null)
                    .setTitle(getString(R.string.select_effect_time_s))
                    .setOnDateSelectedListener(new DateAndTimePickerDialog.OnDateTimeSelectedListener() {
                        @Override
                        public void onDateTimeSelected(String s) {
                            startTime = s;
                            if (endTime != null && 1 == DateUtils.compareDate(startTime + ":00", endTime + ":00")) {
                                ToastUtils.showToast(getString(R.string.set_end_time_info));
                                mConfirm.setEnabled(false);
                                return;
                            } else if (endTime != null) {
                                mConfirm.setEnabled(true);
                            }
                            mStartTime.setText(s);
                        }

                        @Override
                        public void onCancel() {

                        }
                    })
                    .create()
                    .show();


        } else if (i == R.id.tv_end_time) {
            new DateAndTimePickerDialog.Builder(getActivity())
                    .setData(null)
                    .setTitle(getString(R.string.select_effect_time_e))
                    .setOnDateSelectedListener(new DateAndTimePickerDialog.OnDateTimeSelectedListener() {
                        @Override
                        public void onDateTimeSelected(String s) {
                            endTime = s;
                            if ((startTime != null && 1 == DateUtils.compareDate(startTime + ":00", endTime + ":00") || endTime.equals(startTime))) {
                                ToastUtils.showToast(getString(R.string.set_end_time_info));
                                mConfirm.setEnabled(false);
                                return;
                            } else if (endTime != null && 1 == DateUtils.compareDate(getSystemDate(), endTime + ":00")) {
                                ToastUtils.showToast(getString(R.string.set_start_time_info));
                                mConfirm.setEnabled(false);
                                return;

                            } else if (startTime != null) {
                                mConfirm.setEnabled(true);
                            }
                            if (mEndEffect.isSelected()) {
                                mConfirm.setEnabled(true);
                            }
                            mEndTime.setText(s);
                        }

                        @Override
                        public void onCancel() {

                        }
                    })
                    .create()
                    .show();


        } else if (i == R.id.confirm_btn) {
            requestData();

        }
    }

    private void defaultState() {
        mTimingEffect.setSelected(false);
        mEndEffect.setSelected(true);
        mLLEndTime.setVisibility(View.VISIBLE);
        mLLStartTime.setVisibility(View.GONE);
        mEndTime.setText(getString(R.string.select_time));
        mConfirm.setEnabled(false);
        startTime = null;
        endTime = null;
        validType = "1";
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("buId", buId);
        map.put("buName", buName);
        map.put("pbUkid", pbUkid);
        map.put("validEndTime", endTime + ":00");
        if ("2".equals(validType)) {
            map.put("validStartTime", startTime + ":00");
        }
        map.put("validType", validType);
        httpPost(ContractConstant.SAVE_MAIN_CONTRACT, map, 0, true, "");

    }

    public String getSystemDate() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        return sDateFormat.format(new Date());
    }

}
